package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.commlib.base.BaseAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.commlib.base.SyBannerAdapter
import client.comm.commlib.comm_ui.ImageGalleryActivity
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsDetail
import client.comm.baoding.databinding.LayoutGoodsImgItemBinding
import client.comm.baoding.databinding.LayoutGoodsdetailsHeaderBinding
import client.comm.baoding.databinding.LayoutGoodsdetailsHeaderTradeBinding
import client.comm.baoding.dialog.JyInputNumDialog
import client.comm.baoding.ui.GoodDetailImgsActivity
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.commlib.getScreenWidth
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.indicator.CircleIndicator
import java.util.ArrayList

class GoodsDetailTradeAdapter : BaseAdapter<GoodsDetail.Detail> {

    val manager: RequestManager
    lateinit var goodsDetail: GoodsDetail

    constructor(context: Context, manager: RequestManager) : super(context) {
        this.manager = manager
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 0)
            return BindingViewHoder(
                DataBindingUtil.inflate(
                    layoutInflater,
                    R.layout.layout_goodsdetails_header_trade,
                    parent,
                    false
                )
            ).apply {
                val itemBinding = binding as LayoutGoodsdetailsHeaderTradeBinding

            }
        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.layout_goods_img_item,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        if (bindingViewHolder.getItemViewType() === 0) {
            val binding = bindingViewHolder.binding as LayoutGoodsdetailsHeaderTradeBinding

//            binding.toldPrice.text = "￥20"
//            binding.toldPrice.getPaint()
//                .setFlags(Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG) //中划线
//            if (goodsDetail.integral > 0)
//                binding.yxTxt.text = "积分${goodsDetail.integral}"

        } else {
            val binding: LayoutGoodsImgItemBinding =
                bindingViewHolder.binding as LayoutGoodsImgItemBinding
            val path = getItem(position).goods_img
//            binding.path = path
//            binding.event = this
            manager.load(path)
                .into(binding.img)
        }
    }

    fun setData(list: MutableList<GoodsDetail.Detail?>, goodsDetail: GoodsDetail) {
        this.goodsDetail = goodsDetail
        list.add(0, null)
        submitList(list)
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) 0 else 1
    }

//    fun goDy() {
//        try {
//            val intent = Intent()
//            // 93325972684 这个是抖音中的userID 不25是抖音号!!
//            // 93325972684 这个是抖音中的userID 不是抖音号!!
//            intent.data = Uri.parse("snssdk1128://user/profile/88531254126")
//            context.startActivity(intent)
//        } catch (e: Exception) {
//            e.message?.let {
//                if (it.contains("snssdk1128://user/profile/"))
//                    toast("未安装抖音")
//            }
//        }
//    }

    fun itemClick(bean: String) {
        val banners: List<GoodsDetail.Detail?> = getItems()
        val imgs = arrayOfNulls<String>(banners.size - 1)
        for (i in 1 until banners.size) {
            val `in`: Int = i - 1
            val p: String = banners[i]!!.goods_img
            imgs[`in`] = p
        }
        ImageGalleryActivity.show(
            context,
            imgs as Array<String>?,
            imgs.indexOf(bean),
            true,
            false
        )
    }


    fun goZichanJl() {

    }

    fun goZrMyGuaDan() {

    }

    //当前价购买0 / 特价购买1
    fun goDifTypeBuy(type: Int) {
        when (type) {
//            1 -> {//特价购
//                tGoodsDetail?.let {
//                    JyInputNumDialog(this, 1).apply {
//                        ok = { num ->
//                            viewModel.holdBuyTj(it, num)
//                            dismiss()
//                        }
//                    }.show()
//                }
//            }
////            2 -> {//优惠购
////                tGoodsDetail?.let {
////                    JyInputNumDialog(this, 1).apply {
////                        ok = { num ->
////                            viewModel.holdBuyYh(it, num)
////                            dismiss()
////                        }
////                    }.show()
////                }
////            }
//            else -> {//当前购
//                tGoodsDetail?.let {
//                    JyInputNumDialog(this, 1).apply {
//                        ok = { num ->
//                            viewModel.holdBuyYj(it, num)
//                            dismiss()
//                        }
//                    }.show()
//                }
//            }
        }
        /* startActivity(Intent(this, XyzrNewActivity::class.java).apply {
             putExtra("index", 1)
             putExtra("titleName", "")
         })*/
    }


    fun onTihuo() {
//        tGoodsDetail?.let {
//            JyInputNumDialog(this, 2).apply {
//                ok = { num ->
//                    viewModel.holdBuyTh(it, num)
//                    dismiss()
//                }
//            }.show()
//        }
    }


}